package cn.laixueit.controller;

import cn.laixueit.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//@Controller
@Controller
@RequestMapping("/user")
@SessionAttributes(value = "aa")
public class UserController {

    @RequestMapping("/findById")
    public ModelAndView findById(@RequestParam(value = "id",required = true) Integer startpage){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("startpage = " + startpage);
        return modelAndView;
    }

    @RequestMapping("/findById/{id}/{name}")
    public ModelAndView findById2(@PathVariable("id")Integer id,@PathVariable("name")String name,String address){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("id = " + id);
        System.out.println("name = " + name);
        return modelAndView;
    }

    @RequestMapping("/testRequestHeader")
    public ModelAndView testRequestHeader(@RequestHeader("Accept")String accept){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("accept = " + accept);
        return modelAndView;
    }

    @RequestMapping("/testCookie")
    public ModelAndView testCookie(@CookieValue("jsessionid")String jsessionid){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("JSSESSIONID = " + jsessionid);
        return modelAndView;
    }

    @ModelAttribute
    public void testmodelattribute(Model model){
        model.addAttribute("aa","hello world");
        System.out.println(123);

    }

    @RequestMapping("/testmodelpar")
    public ModelAndView testmodelpar(@ModelAttribute("aa") String aa){
        ModelAndView modelAndView = new ModelAndView("/index.jsp");
        modelAndView.addObject("aa",aa);
        return modelAndView;
    }

    @RequestMapping("/testrequestbody")
    public ModelAndView testrequestbody(@RequestBody String body){
        ModelAndView modelAndView = new ModelAndView("/index.jsp");
        System.out.println("body = " + body);
        return modelAndView;
    }

    @RequestMapping("/testrequestbodyUser")
    public ModelAndView testrequestbody(@RequestBody User user){
        ModelAndView modelAndView = new ModelAndView("/index.jsp");
        System.out.println("user = " + user);
        return modelAndView;
    }

    @RequestMapping("/testResponseBody")
    public @ResponseBody User testResponseBody(User user) {
        System.out.println("user = " + user);
        return user;
    }

    @RequestMapping("/testreturnvoid")
    public void testvoid(){

    }

    @RequestMapping("/testreturntring")
    public String testreturntring(){
        //  /WEB-INF/index.jsp
        return "index";
    }

    @RequestMapping("/testredirect")
    public String testredirect(){
        //  /WEB-INF/index.jsp
        return "redirect:/index.jsp";
    }


    @RequestMapping("/testredirect2")
    public String testredirect2(){
        //  /WEB-INF/index.jsp
        return "redirect:findById?id=2";
    }

    public String testservlet(HttpServletRequest request, HttpServletResponse response, HttpSession session){

        return "";
    }

}
